package com.caszx.chargingpile.fragment.battery;


import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONException;
import com.app.base.adapter.BaseClickRecyclerAdapter;
import com.app.base.adapter.BaseHeadFootAdapter;
import com.app.base.fragment.BaseNetViewFragment;
import com.app.jsonparse.JSONParse;
import com.app.utils.DiskUser;
import com.app.utils.DividerGridItemDecoration;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.activity.BatteryChargeActivity;
import com.caszx.chargingpile.activity.UserSignInActivity;
import com.caszx.chargingpile.entity.EntityCharge;
import com.caszx.chargingpile.eventbus.EventBusAction;
import com.caszx.chargingpile.holder.battery.ChargeHolder;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.utils.DialogHelp;
import com.rey.material.app.DialogFragment;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;


public class ChargeListHomeFragment extends BaseNetViewFragment {


    private RecyclerView mRecyclerView;
    private BaseHeadFootAdapter<EntityCharge> mRecyclerAdapter;

    public static ChargeListHomeFragment newInstance() {
        ChargeListHomeFragment fragment = new ChargeListHomeFragment();
        return fragment;
    }


    @Override
    protected void onLoad() {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(getActivity(), "token"));
        OkHttpUtils.post()
                .url(ConstantUrl.URL_LIST_CHARGE)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        showErrorView(R.string.error_network);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        JSONObject resultJsonObject = response.optJSONObject("result");
                        if (resultJsonObject != null) {
                            int code = resultJsonObject.optInt("code");
                            if (code == -100) {
                                DialogHelp.showDialogOnlyMesg(getActivity(), getResources().getString(R.string.error_minus_100), new DialogHelp.DialogActionClickListener() {
                                    @Override
                                    public void onPositiveActionClicked(DialogFragment fragment) {
                                        Intent intent = new Intent(getActivity(), UserSignInActivity.class);
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onNegativeActionClicked(DialogFragment fragment) {
                                    }
                                });
                                showErrorView(getResources().getString(R.string.error_minus_100));
                                return;
                            }
                            if (code < 0) {
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                showErrorView(resultJsonObject.optString("msg"));
                            } else {
                                try {
                                    showContentView();
                                    List<EntityCharge> charges = JSONParse.parseArray(response.optString("chargingList"), EntityCharge.class);
                                    mRecyclerAdapter.setDatas(charges);
                                    if (mRecyclerAdapter.getItems() == null || mRecyclerAdapter.getItems().size() == 0) {
                                        showErrorView("暂无充电数据");
                                    }
                                } catch (JSONException e) {
                                    showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected View initOnloadedView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.common_recyclerview, null);
        initRecyclerView(view);
        return view;
    }

    private void initRecyclerView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerAdapter = new BaseHeadFootAdapter<>(R.layout.item_chagre, ChargeHolder.class);
        mRecyclerView.setAdapter(mRecyclerAdapter);
        mRecyclerAdapter.setOnItemClickListener(new BaseClickRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), BatteryChargeActivity.class);
                intent.putExtra("ID", mRecyclerAdapter.getItems().get(position).ordersId);
                startActivity(intent);

            }
        });

    }


}
